﻿#coding=utf-8
# common test 

import urllib2
import os
from utils import test

url = 'http://223.4.152.92:9071/WIFPa/extension/config?hwid=805926'
url2 = 'http://www.greenteapress.com/thinkpython/code'
# 如何批量下载url2的所有py文件

# req = urllib2.Request(url2)
# resp = urllib2.urlopen(req)
# html = resp.read()
# print html

# -*- coding: utf-8 -*-
# Filename: BatchChangeFileName.py

import os
import sys

def TraverseFolder(path):
    print "<----folder&file---->"
    no=0
    for (path,dirs,files) in os.walk(path):
        print
        no += 1
        print "No.%d"%no
        print "path=%s"%path
        if len(dirs)!=0:
            #print type(dirs)
            subfolders = ''
            for dir in dirs:
                subfolders += dir+';'
            subfolders = '[' + subfolders + ']'
            print "subfolders=%s"%subfolders
        if len(files)!=0:
            filenames = ''
            for filename in files:
                filenames += filename+';'
            filenames = '[' + filenames + ']'
            print "files=%s"%filenames
    print "<----folder&file---->"
    

def RenameFile():
    str = u'请输入要处理的文件夹路径====>'
    path = raw_input(str.encode('gbk'))
    print path
    
    str = u'请输入源文件类型(不包括.)====>'
    old_ext = "."+raw_input(str.encode('gbk'))
    print old_ext
    
    str = u'请输入目标文件类型(不包括.)====>'
    new_ext = "."+raw_input(str.encode('gbk'))    
    print new_ext
    
    print   #输出空行占位
    TraverseFolder(path)
    print
    
    str = u'开始批量更名'
    print str
    print '<-----------------'
    changedCount = 0          
    for (path,dirs,files) in os.walk(path):
        for filename in files:
            ext = os.path.splitext(filename)[1] #取得文件类型，注意它还带着点号            
            if (ext == old_ext):
                changedCount += 1
                newname = filename.replace(old_ext, new_ext)
                oldpath = path + "\\" + filename      
                newpath = path + "\\" + newname
                try:
                    os.rename(oldpath, newpath)
                    print 'No.%d'%changedCount, 'change', oldpath, 'to', newpath
                except BaseException, e:  
                    print(str(e))
    print '----------------->'
                                                                               
def story(**kwds):
    return 'Once upon a time,there was a %(job)s called %(name)s.' %kwds

def power(x,y,*others):
    if others:
        print 'Received redundant parameters:',others
    return pow(x,y)

def interval(start,stop=None,step=1):
    'Imitates range() for step>0'
    if stop is None:
        start,stop = 0,start
    result = []
    i = start
    while i < stop:
        result.append(i)
        i += step
    return result

def test1():
    #RenameFile() 
    #TraverseFolder('d:\\mydata\\paper')
    print story(job='king',name='Jim')
    print story(name='Tom',job='king')    
    params = {'job':'cshi','name':'python'}
    print story(**params)

    power(3,2)
    power(y=3,x=2)
    params = (5,)*2
    power(*params)

    interval(10)
    power(*interval(3,7))


if __name__ == '__main__':
    i=0
    print type(i)        
    raw_input("press Enter to exit: ")

    # must add __init__.py if call other folders' modules, or use sys.path.append
    # https://blog.csdn.net/qq_28072715/article/details/80939699
    test.func_in_utils()
    sys.exit()
